package com.raos.api;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

/**
 * 创建 ZNode节点 测试（重复操作时需删除zk中除zk自身节点外的其他节点）
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2022/9/5 21:04
 */
public class CreateZNode {

    private ZooKeeper zooKeeper;

    public CreateZNode(ZooKeeper zooKeeper) {
        this.zooKeeper = zooKeeper;
    }

    // 同步创建节点
    public void createZNodeWithSync() throws Exception {
        String zNode = zooKeeper.create("/zookeeper-api-sync", "111".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        System.out.println("创建节点成功: " + zNode);
    }

    // 异步创建节点
    public void createZNodeWithAsync() {
        zooKeeper.create("/zookeeper-api-async", "111".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT, new AsyncCallback.StringCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, String name) {
                System.out.println("rc: " + rc);
                System.out.println("path: " + path);
                System.out.println("ctx: " + ctx);
                System.out.println("name: " + name);
            }
        }, "create-asyn");
    }

    public static void main(String[] args) throws Exception {
        CreateZNode createZNode = new CreateZNode(ZkConnUtil.getZkConn("192.168.235.129:2181"));
        createZNode.createZNodeWithSync();
        createZNode.createZNodeWithAsync();
        System.in.read();
    }

}